/**
 * related to list.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-11-21
 */
var page = sm("do_Page");
var app = sm("do_App");
var hashdata = mm("do_HashData");
var rootview = ui("$");
var nf = sm("do_Notification");
var common = require("common");
var orderUtil = require("url/order");
var qqHttp = require("helper/http");
var do_ListView_order = ui("do_ListView_order");
var listdata = mm("do_ListData");
do_ListView_order.bindItems(listdata);
var noData = [{"template":1,"labelContent":"您还没有订单"}];
var pageNum = 1, totalPages = 1;

//andriod返回键
page.on("back", function() {
    app.closePage();
});

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "我的配送"
})
header.refreshData();

var topIndex = 0;
var topAout0 = ui("aLayoutTop0");
var topAout1 = ui("aLayoutTop1");
var topAout2 = ui("aLayoutTop2");
var topAout = [ topAout0, topAout1, topAout2];

var labelTop0 = ui("labelTop0");
var labelTop1 = ui("labelTop1");
var labelTop2 = ui("labelTop2");
var topLabel = [ labelTop0, labelTop1, labelTop2];

var labelLine0 = ui("labelLine0");
var labelLine1 = ui("labelLine1");
var labelLine2 = ui("labelLine2");
var labelLine = [ labelLine0, labelLine1, labelLine2];

topAout.forEach(function(foot, tabIndex) {
	foot.on("touch", function(data, e) {
		if (topIndex !== tabIndex) {
			topLabel[topIndex].fontColor = "949EAAFF";
			labelLine[topIndex].bgColor = "00000000";
			topLabel[tabIndex].fontColor = "FF7E00FF";
			labelLine[tabIndex].bgColor = "FF7E00FF";
			refreshAllData(tabIndex);
		}
		topIndex = tabIndex;
	});
});

function getParam(tabIndex) {
	if (tabIndex == 0) {
		return {"hasSendDelivery":2};
	} else if (tabIndex == 1) {
		return {"hasSendDelivery":1};
	} else {
		return {"parentSendDeliveryUser":1};
	}
}

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var data = page.getData();
	if (data && data.canAssigned == 0) {
		topAout2.visible = false;
	}
	refreshAllData(0);
});

page.on("result", function(data) {
	if(data && data.fromPage){
		if(data.fromPage==="sendDeliveryUser"){
			refreshAllData();
		} else if(data.fromPage==="sendDeliveryOrderDetail"){
			var strSendDeliveryStatus = "";
			if (data.sendDeliveryStatus == 1) {
				strSendDeliveryStatus = "待配送";
			} else if (data.sendDeliveryStatus == 2) {
				strSendDeliveryStatus = "配送中";
			} else if (data.sendDeliveryStatus == 3) {
				strSendDeliveryStatus = "已配送";
			}
			var data_temp = listdata.getRange(0);
			listdata.updateOne(data.index, {"orderNo":data_temp[data.index].orderNo,"fullImg":data_temp[data.index].fullImg,"title":data_temp[data.index].title,
				"orderType":data_temp[data.index].orderType,"strNewAt":data_temp[data.index].strNewAt,"orderStatus":data_temp[data.index].orderStatus,
				"total":data_temp[data.index].total,"sendDeliveryStatus":data.sendDeliveryStatus,"buyerNick":data_temp[data.index].buyerNick,
				"strSendDeliveryStatus":strSendDeliveryStatus});
			do_ListView_order.refreshItems();
		}
	}
});

//刷新数据
function refreshAllData(tabIndex){
	pageNum = 1;
	var param = getParam(tabIndex);
	param.page = pageNum;
	qqHttp.post(orderUtil.url.base.deliveryPage, param, function(data) {
		labelTop0.text = '待配送('+data.notSendDeliveryC+')';
		labelTop1.text = '已配送('+data.sendDeliveryC+')';
		labelTop2.text = '我指派('+data.parentSendDeliveryC+')';
		totalPages = data.page.totalPages;
		do_ListView_order.rebound();
		listdata.removeAll();
		if (data.page.content.length) {
			img_domain = data.page.content[0].fileDomain;
			listdata.addData(data.page.content);
		}
		else{
			listdata.addData(noData);
		}
		do_ListView_order.bindItems(listdata);
		do_ListView_order.refreshItems();
	})
}

function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		var param = getParam(topIndex);
		param.page = pageNum;
		qqHttp.post(orderUtil.url.base.deliveryPage, param, function(data) {
			if (data.page.content.length > 0) {
				img_domain = data.page.content[0].fileDomain;
			}
			totalPages = data.page.totalPages;
			listdata.addData(data.page.content);
			do_ListView_order.rebound();
			do_ListView_order.refreshItems();
		})
	} else {
		nf.toast("已是最后一页");
		do_ListView_order.rebound();
		do_ListView_order.refreshItems();
	}
}

//下拉列表，刷新数据
do_ListView_order.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData(topIndex);
	}
});

//上拉列表，翻页数据
do_ListView_order.on("push", function(data){
	//其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){	
		getNextPageData();
	}
});
